package com.xhj.arithmetic;
/**
 * -
 *
 * @author 周沫
 * @Date 2021-07-18
 * @Time 14:47
 */
public class A0002 {

	public static void main(String[] args) {
		ListNode l1 = new ListNode(2);
		l1.next = new ListNode(4);
		l1.next.next = new ListNode(3);

		ListNode l2 = new ListNode(5);
		l2.next = new ListNode(6);
		l2.next.next = new ListNode(4);

		ListNode l3 = new Solution().addTwoNumbers2(l1, l2);

		System.out.println("l1: " + l1.toString());
		System.out.println("l2: " + l2.toString());
		System.out.println("l3: " + l3.toString());
	}
}
class ListNode {

	int val;

	ListNode next;

	ListNode() {
	}

	ListNode(int val) {
		this.val = val;
	}

	ListNode(int val, ListNode next) {
		this.val = val;
		this.next = next;
	}

	public String toString() {
		String str = String.valueOf(this.val);
		ListNode now = this;
		while (now.next != null) {
			str += now.next.val;
			now = now.next;
		}
		return new StringBuilder(str).reverse().toString();
	}
}
class Solution {

	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode l3 = new ListNode(0);
		ListNode l1now = l1;
		ListNode l2now = l2;
		ListNode l3now = l3;
		ListNode lr = new ListNode();
		class ListNodeUtil {

			ListNode result = new ListNode();

			ListNode doAdd(ListNode lr, int num) {
				if (lr != null) {
					result.val = lr.val;
					lr = lr.next;
					doAdd(lr, lr.val);
				}
				return result;
			}
		}
		//首位计算（个位）
		if ((l1now.val + l2now.val) > 9) {
			l3now.val = (l1now.val + l2now.val) - 10;
			l3now.next = new ListNode(1);
		} else {
			l3now.val = l1now.val + l2now.val;
		}
		//如果存在后位，步入计算。层层刷新也
		while (l1now.next != null || l2now.next != null || l3now.next != null) {
//			l1now = l1now.next;
//			l2now = l2now.next;
//			l3now = l3now.next;//更新所有节点
			//一路二路都有后
			if (l1now.next != null && l2now.next != null) {
				l1now = l1now.next;
				l2now = l2now.next;// - - - - - - -"节点更新"
				if (l3now.next == null) {
					if ((l1now.val + l2now.val) > 9) {
						l3now.next = new ListNode(l1now.val + l2now.val - 10);// - - - - - - -"三路追加节点"
						l3now = l3now.next;// - - - - - - -"三路刷新，与一二路追平"
						l3now.next = new ListNode(1);// - - - - - - -"三路后位进一"
					} else {// <= 9
						l3now.next = new ListNode(l1now.val + l2now.val);
						l3now = l3now.next;// - - - - - - -"三路刷新，与一二路追平"
					}
				} else {//l3now.next!=null
					l3now = l3now.next;// - - - - - - -"三路刷新，与一二路追平"
					if ((l1now.val + l2now.val + l3now.val) > 9) {
						l3now.val = l1now.val + l2now.val + l3now.val - 10;
						l3now.next = new ListNode(1);
					} else {// <= 9
						l3now.val = l1now.val + l2now.val + l3now.val;
					}
				}
			}
			//一二路为空，三路必存也
			else if (l1now.next == null && l2now.next == null) {
				l3now = l3now.next;// - - - - - - -"三路节点刷新"
				break;// - - - - - - -"终止循环"
			}
			//一二路有一路为空
			else if (l1now.next != null) {
				l1now = l1now.next;
				if (l3now.next == null) {
					l3now.next = l1now;
					l3now = l3now.next;// - - - - - - -"跟进（若有break，此行可略）"
					break;
				} else {
					l3now = l3now.next;
					if (l3now.val + l1now.val > 9) {
						l3now.val = l3now.val + l1now.val - 10;
						l3now.next = new ListNode(1);
					} else {
						l3now.val = l3now.val + l1now.val;
					}
				}
			} else {
				l2now = l2now.next;
				if (l3now.next == null) {
					l3now.next = l2now;
					l3now = l3now.next;
					break;
				} else {
					l3now = l3now.next;
					if (l3now.val + l2now.val > 9) {
						l3now.val = l3now.val + l2now.val - 10;
						l3now.next = new ListNode(1);
					} else {
						l3now.val = l3now.val + l2now.val;
					}
				}
			}
		}
		return l3;
	}

	public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
		ListNode l3 = new ListNode();
		class Util {
			String str = "";
			ListNode tempL;
			char[] chars;
			int toInt(ListNode ll) {
				tempL=ll;
				while (tempL != null) {
					str += tempL.val;tempL=tempL.next;
				}
				return Integer.valueOf(new StringBuilder(str).reverse().toString());
			}
			ListNode toListNode(int num) {
				chars = new StringBuilder(String.valueOf(num)).reverse().toString().toCharArray();
				ListNode ll;
				for (int i = 0; i < chars.length; i++) {
					ll = new ListNode(Integer.valueOf(String.valueOf(chars[i])));// - - - - - - -"当前节点赋值"
					if (i == 0) {
						tempL = ll;// - - - - - - -"记录首位地址值"
					}
					ll = ll.next;// - - - - - - -"当前节点后移，以备下次使用"
				}
				return tempL;
			}
		}
		int num1 = new Util().toInt(l1);
		int num2 = new Util().toInt(l2);
		l3 = new Util().toListNode(num1 + num2);
		return l3;
	}
}